package utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class DataMiner {
	private final static Logger  LOGGER = Logger.getLogger(DataMiner.class.getName());   
	Map<String, Integer> colorAmountMap;

		
	public DataMiner(){
		colorAmountMap = new TreeMap<>();
	}
	
	
	private void convertToDom(String URL){
		DOMParser parser = new DOMParser();
		
		try {
			parser.parse(new InputSource(new URL(URL).openStream()));
			Document doc = parser.getDocument();
			
			NodeList nodeList = doc.getElementsByTagName("Item");
			for (int i = 0; i < nodeList.getLength(); i++) {
				System.out.print("Item "+(i+1));
				Node n = nodeList.item(i);
				NamedNodeMap m = n.getAttributes();
				System.out.print(" Name: "+m.getNamedItem("Name").getTextContent());
				System.out.print(" Type: "+m.getNamedItem("Type").getTextContent());
				Node actualNode = n.getFirstChild();
				if (actualNode != null) {
					System.out.println(" "+actualNode.getNodeValue());
				} else {
					System.out.println(" ");                    
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	/**@Deprecated 
	 * Gets a string of the HTML code of the "ipString" html page
	 * 
	 * @param ipString - valid IP address with HTTP port(80) enabled
	 * @return String containing page source code
	 * @throws IOException - (BufferReader)
	 */
	private String getUrlSource(String ipString) throws IOException {
		URL ip = new URL(ipString);
		URLConnection ipConn = ip.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(
				ipConn.getInputStream(), "UTF-8"));
		String inputLine;
		StringBuilder a = new StringBuilder();
		while ((inputLine = in.readLine()) != null)
			a.append(inputLine);
		in.close();

		return a.toString();
	}
	
	

	/**@Deprecated
	 * Gets values for available 
	 * 
	 * @param ipString
	 * @throws IOException
	 */
	public void findCartegesProcent(String ipString) throws IOException {
		
		String context = getUrlSource(ipString);
		LOGGER.log(Level.INFO, "page context: \n"  + context);
	
		String regexTagContent = "(<td{1}>)([^<][^&nbsp;&nbsp;].*?)(</td>)";
		String regexValContent = "\\d+";
		
		Pattern patternTagContent = Pattern.compile(regexTagContent);
		Matcher matcherTagContent = patternTagContent.matcher(context);

		Pattern patternValContent = Pattern.compile(regexValContent);
		while(matcherTagContent.find()){
			Matcher matcherValContent = patternValContent.matcher(matcherTagContent.group(2));
			matcherValContent.find();
			LOGGER.log(Level.INFO, "match: "  + matcherTagContent.group(2)+ "\n\t\t ----- " + matcherValContent.group());
		}
	}

	
	public static void main(String[] args) {
		DataMiner dm = new DataMiner();
		try {
//			dm.findCartegesProcent("http://10.130.2.64/");
			dm.convertToDom("http://www.regular-expressions.info/examples.html");
		} 
//		catch (IOException e) {
//			e.printStackTrace();
//		}
		catch(Exception e){
			e.getMessage();
			e.printStackTrace();
			
		}
	}

}
